package com.yycx.module.weixin.provider.controller;

import com.yycx.common.mybatis.model.ResultBody;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.*;

/**
 * jsapi 演示接口的 controller.
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 * @date 2020-04-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/common/wx/jsapi/{appid}")
public class WxJsapiController {
    private final WxMpService wxService;

    @PostMapping("/getWxJsapiSignature")
    public WxJsapiSignature getWxJsapiSignature(@PathVariable String appid, String url) throws WxErrorException {
        WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(appid).createJsapiSignature(url);
        return jsapiSignature;
    }

    @GetMapping("/getSignature")
    public ResultBody getSignature(@PathVariable String appid, String url) throws WxErrorException {
        WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(appid).createJsapiSignature(url);
        return ResultBody.ok(jsapiSignature);
    }

    @GetMapping("/getJsapiTicket")
    public String getJsapiTicket(@PathVariable String appid, String url) throws WxErrorException {
        WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(appid).createJsapiSignature(url);
        System.out.println(jsapiSignature);
        return this.wxService.getJsapiTicket(true);
    }
}
